﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Game
{
    public class Map : VisibleGameEntity
    {
        public override bool Init(ContentManager Content, int n, string strResource, int dframe, int nframe)
        {
            _nSprite = 1;
            _Sprite = new MySprite[_nSprite];
            Texture2D texture2D;

            texture2D = Content.Load<Texture2D>(@"Map/" + strResource);


            _Sprite[0] = new MySprite(texture2D, nframe, dframe, 100.0f, 100.0f);

            return true;
        }

        public override VisibleGameEntity Clone()
        {
            VisibleGameEntity newObject = new Map();
            newObject._nSprite = this._nSprite;
            newObject._Sprite = new MySprite[this._nSprite];
            for (int i = 0; i < this._nSprite; i++)
            {
                newObject._Sprite[i] = new MySprite(this._Sprite[i].texture2d, this._Sprite[i].nFrame, this._Sprite[i].dFrame, X, Y); 
            }
            newObject.X = this.X;
            newObject.Y = this.Y;
            return newObject;
        }

        public delegate void KeyPressedHandler(object sender,
   Keys key);
        public event KeyPressedHandler KeyPressed;


        Keys[] prevKeys = new Keys[0];
        public Keys GetNewKeyPressed(Keys[] prevKeys, Keys[] keys)
        {
            throw new NotSupportedException("Coding by yourself");
        }

        public void updatekey(GameTime gameTime, int select)
        {
            // Get state of the keyboard
            KeyboardState state = Keyboard.GetState();

            // Do events
            if (this.KeyPressed != null)
                this.KeyPressed(this, this.GetNewKeyPressed(
                    prevKeys, state.GetPressedKeys()));
            prevKeys = state.GetPressedKeys();

            // Moving with iteraction
            if (state.IsKeyDown(Keys.Left))
            {

                this._Sprite[select].x -= 5;

            }
            else if (state.IsKeyDown(Keys.Right))
            {

                this._Sprite[select].x += 5;

            }
            else if (state.IsKeyDown(Keys.Up))
            {

                this._Sprite[select].y -= 5;

            }
            else if (state.IsKeyDown(Keys.Down))
            {

                this._Sprite[select].y += 5;

            }
            //for (int i = 0; i < this._nSprite; i++)
            //{
            //    this._Sprite[i].itexture2d = (this._Sprite[i].itexture2d + 1) % this._Sprite[i].ntexture2d;
            //}
            //  _itexture2d = (_itexture2d + 1) % ntexture2d;
            //  this._Sprite[select].itexture2d = (this._Sprite[select].itexture2d + 1) % this._Sprite[select].ntexture2d;

        }
    }
}
